﻿


using Admin.NET.Application.ServiceTra.TraFaXues.Dto;
using Admin.NET.Core.Btms;
using Mapster;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;

namespace Admin.NET.Application.Service;
public class TraFaXueMingXiService : BaseService<TraFaXueMingXi>
{
    private readonly SqlSugarRepository<TraFaXue> _traShenQingFaXueZhuBiaoRep;
    private readonly SqlSugarRepository<TraFaXueMingXi> _traShenQingFaXueMingXiRep;
    private readonly SqlSugarRepository<InvKuCun> _invKuCunRep;
    public TraFaXueMingXiService(
        SqlSugarRepository<TraFaXue> traShenQingFaXueZhuBiaoRep,
        SqlSugarRepository<TraFaXueMingXi> traShenQingFaXueMingXiRep,
        SqlSugarRepository<InvKuCun> invKuCunRep,
        IServiceScopeFactory scopeFactory
    ) : base(scopeFactory)
    {
        _traShenQingFaXueZhuBiaoRep = traShenQingFaXueZhuBiaoRep;
        _traShenQingFaXueMingXiRep = traShenQingFaXueMingXiRep;
        _invKuCunRep = invKuCunRep;
    }

    /// <summary>
    /// 通过Id获取主表与明细列表
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<TraFaXueMessageDto> GetMessage(string id)
    {
        var zhuBiao = await _traShenQingFaXueZhuBiaoRep.AsQueryable().FirstAsync(t => t.Id == id);
        var mingXiList = await _traShenQingFaXueMingXiRep.AsQueryable()
            .Where(e => e.traFaXueId == id)
            .InnerJoin<InvKuCun>((faXueMingXi, InvKuCun) => faXueMingXi.xueYeId == InvKuCun.Id)
            .Select((faXueMingXi, invKuCun) => new
            {
                faXueMingXi.Id,
                faXueMingXi.xueYeId,
                faXueMingXi.traFaXueId,
                faXueMingXi.traPeiXueMingXiId,
                faXueMingXi.shiFouTuiXue,
                invKuCun,
            })
            .ToListAsync();
        var resultMingXiList = mingXiList.Select(e => new TraFaXueMingXiOutput()
        {
            Id = e.Id,
            xueYeId = e.xueYeId,
            traFaXueId = e.traFaXueId,
            traPeiXueMingXiId = e.traPeiXueMingXiId,
            shiFouTuiXue = e.shiFouTuiXue,
            invKuCun = e.invKuCun.Adapt<InvKuCunOutput>()
        })
        .ToList();
        return new TraFaXueMessageDto()
        {
            ZhuBiao = zhuBiao.Adapt<TraFaXueOutput>(),
            MingXiList = resultMingXiList
        };
    }


    public TraFaXueMingXi AddMingXi(TraFaXueMingXiInput input)
    {
        var entity = input.Adapt<TraFaXueMingXi>();
        return _rep.InsertReturnEntity(entity);


    }

    public void DelMingXi(long? id)
    {
        _rep.Delete(t => t.Id == id);

    }

    public bool UpdateMingXi(TraFaXueMingXiInput input)
    {
        return _rep.Update(input.Adapt<TraFaXueMingXi>());
    }


}
